Load Libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.1.8
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(here)
## here() starts at C:/Users/mhein/OneDrive/Desktop/Computer Modeling R Programs/Hein
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
library(mapdata)
library(mapproj)
library(ggmap)
## ℹ Google's Terms of Service: <]8;;https://mapsplatform.google.comhttps://mapsplatform.google.com]8;;>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(ggsn)
## Loading required package: grid
library(patchwork)
## Warning: package 'patchwork' was built under R version 4.2.3
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.2.3
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.2.3
library(magick)
## Warning: package 'magick' was built under R version 4.2.3
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
library(ggvis)
## Warning: package 'ggvis' was built under R version 4.2.3
## 
## Attaching package: 'ggvis'
## 
## The following object is masked from 'package:gganimate':
## 
##     view_static
## 
## The following object is masked from 'package:ggplot2':
## 
##     resolution

Load Data

ChemData <- read_csv(here("Week_09","Data","chemicaldata_maunalua.csv"))
## Rows: 23 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): Zone, Site, Season, Tide_time
## dbl (11): Waypoint, Lat, Long, Temp_in, Salinity, Phosphate, Silicate, NN, p...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ChemDataDictionary <- read_csv(here("Week_09","Data","chem_data_dictionary.csv"))
## Rows: 15 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Variable, Description, Units
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Data Analysis

View(ChemData)
View(ChemDataDictionary)

mol_to_g: a function that converts mol, mmol, or umol to ug, mg, or g

silicate molar mass = 76.083 g/mol phosphate molar mass = 94.9714 g/mol

v mol * (1 mol / 1 mol) = w mol, in_conv = 1 v mmol * (1 mol / 1000 mmol) = w mol, in_conv = 1/1000 = 0.001 v umol * (1 mol / 1000000 umol) = w mol, in_conv = 1/1000000 = 0.000001

w mol * x g/mol = y g

y g * (1 g / 1 g) = z g, out_conv = 1 y g * (1000 mg / 1 g) = z mg, out_conv = 1000 y g * (1000000 ug / 1 g) = z ug, out_conv = 1000000

mol_to_g <- function(
    var_in, # variable to convert
    in_units, # convert from moles ("mol"), millimoles ("mmol"), or micromoles ("umol")
    mw, # molecular weight of substance
    out_units # convert to micrograms ("ug"), milligrams ("mg"), or grams ("g")
    ){
  if(in_units == "mol"){
    in_conv = 1
    }
  else if(in_units == "mmol"){
    in_conv = 0.001
    }
  else if(in_units == "umol"){
    in_conv = 0.000001
    }
  if(out_units == "g"){
    out_conv = 1
    }
  else if(out_units == "mg"){
    out_conv = 1000
    }
  else if(out_units == "ug"){
    out_conv = 1000000
    }
  var_out = var_in * in_conv * mw * out_conv
  return(var_out) # converted variable
  }

Testing mol_to_g()

mol_to_g(50, "mol", 100, "g")
## [1] 5000
mol_to_g(50, "mmol", 100, "g")
## [1] 5
mol_to_g(50, "umol", 100, "g")
## [1] 0.005
mol_to_g(50, "mol", 100, "mg")
## [1] 5e+06
mol_to_g(50, "mmol", 100, "mg")
## [1] 5000
mol_to_g(50, "umol", 100, "mg")
## [1] 5
mol_to_g(50, "mol", 100, "ug")
## [1] 5e+09
mol_to_g(50, "mmol", 100, "ug")
## [1] 5e+06
mol_to_g(50, "umol", 100, "ug")
## [1] 5000

map_point: a function that makes a ggmap of a chosen area, and then plots points over it based on a chosen dataset

map_point <- function(
    lon, # longitude for base map
    lat, # latitude for base map
    data, # dataset to plot
    x, # column from dataset to plot on x axis 
    y, # column from dataset to plot on y axis 
    var, # column from dataset to represent at each point
    zoom, # zoom level of the map
    maptype = "satellite" # type for base map, satellite map by default
    ){
  # Make a data frame of lon and lat coordinates
  map_coor <- data.frame(lon = {{lon}}, lat = {{lat}})
  # Get base layer
  base_map <- get_map(map_coor, zoom = {{zoom}}, maptype = {{maptype}})
  # Plot it
  chem_map <- ggmap(base_map) +
    geom_point(data = {{data}}, aes(x = {{x}}, y = {{y}}, color = {{var}}), size = 4) +
    scale_color_viridis_c() +
    labs(x = "Longitude", y = "Latitude")
  return(chem_map)
  }

Make converted columns for ChemData

ChemData_converted <- ChemData %>%
  mutate(Phosphate_ug = mol_to_g(Phosphate, "umol", 94.9714, "ug")) %>%
  mutate(Phosphate_mg = mol_to_g(Phosphate, "umol", 94.9714, "mg")) %>%
  mutate(Phosphate_g = mol_to_g(Phosphate, "umol", 94.9714, "g")) %>%
  mutate(Silicate_ug = mol_to_g(Silicate, "umol", 76.083, "ug")) %>%
  mutate(Silicate_mg = mol_to_g(Silicate, "umol", 76.083, "mg")) %>%
  mutate(Silicate_g = mol_to_g(Silicate, "umol", 76.083, "g"))

View(ChemData_converted)
phosphate_plot_umol <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Phosphate, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
phosphate_plot_g <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Phosphate_g, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
phosphate_plot_mg <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Phosphate_mg, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
phosphate_plot_ug <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Phosphate_ug, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
silicate_plot_umol <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Silicate, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
silicate_plot_g <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Silicate_g, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
silicate_plot_mg <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Silicate_mg, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
silicate_plot_ug <- map_point(lon = -157.7621, lat = 21.27427, data = ChemData_converted, x = Long, y = Lat, var = Silicate_ug, zoom = 17, maptype = "watercolor")
## ! `maptype = "watercolor"` is only available with `source = "stamen"`; resetting source.
## ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=21.27427,-157.7621&zoom=17&size=640x640&scale=2&maptype=terrain&key=xxx]8;;>
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
phosphate_plot_umol
## Warning: Removed 2 rows containing missing values (`geom_point()`).

phosphate_plot_g 
## Warning: Removed 2 rows containing missing values (`geom_point()`).

phosphate_plot_mg 
## Warning: Removed 2 rows containing missing values (`geom_point()`).

phosphate_plot_ug
## Warning: Removed 2 rows containing missing values (`geom_point()`).

silicate_plot_umol
## Warning: Removed 2 rows containing missing values (`geom_point()`).

silicate_plot_g 
## Warning: Removed 2 rows containing missing values (`geom_point()`).

silicate_plot_mg 
## Warning: Removed 2 rows containing missing values (`geom_point()`).

silicate_plot_ug
## Warning: Removed 2 rows containing missing values (`geom_point()`).